package com.xjh.mange.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.mange.common.constant.UserSessionName;
import com.xjh.mange.common.util.SessionUtil;
import com.xjh.mange.model.Picture;
import com.xjh.mange.model.User;
import com.xjh.mange.service.LogService;
import com.xjh.mange.service.PictureService;
import com.xjh.mange.vo.LayuiTableVo;
import com.xjh.mange.vo.PictureDto;
import com.xjh.mange.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @author 徐佳辉
 * @date 2020/12/23 11:29
 */
@Controller
@RequestMapping("/picture")
@Slf4j
public class PictureController {
    @Autowired
    private PictureService pictureService;
    @Value("${picture.url}")
    private String PicUrl;
    @Autowired
    private LogService logService;

    /**
     * 上传物品图片
     *
     * @param file
     * @return
     */
    @PostMapping("/up")
    @ResponseBody
    public ResponseVO up(MultipartFile file, HttpSession session) {
        HashMap hashMap = new HashMap();
        String url = null;
        try {
            url = pictureService.up(file, PicUrl);
        } catch (IOException e) {
            log.error("物品图片上传出错");
            e.printStackTrace();
            return ResponseVO.error("物品图片上传出错");
        }
        hashMap.put("url", url);
        logService.Log(SessionUtil.getName(session), "上传图片:" + url);
        return ResponseVO.ok(url);
    }



    /**
     * 物品图片分页
     *
     * @param page
     * @param limit
     * @param name
     * @param session
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public LayuiTableVo list(@RequestParam(required = true, defaultValue = "1") int page, @RequestParam(required = true, defaultValue = "8") int limit, String name, HttpSession session) {
        log.info("page:{},limit:{},name:{}", page, limit, name);
        if (page < 1) {
            page = 1;
        }
        if (limit < 1) {
            limit = 8;
        }
        User u = (User) session.getAttribute(UserSessionName.USER);
        if (u == null) {
            return LayuiTableVo.error("请登录");
        }
        Page<PictureDto> pictureDtoPage = new Page<>(page, limit);
        List<PictureDto> pictureDtoIPage = pictureService.selectAllByUserIdOrName(pictureDtoPage, u.getUserId(), name);
        for (PictureDto record : pictureDtoIPage) {
            if (record.getAddress() == null) {
                record.setAddress("../imgs/15.jpg");
            }
        }
        return LayuiTableVo.ok(pictureDtoIPage, (int) pictureDtoPage.getTotal());
    }


    /**
     * 图片地址上传
     *
     * @param id
     * @param url
     * @param session
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public ResponseVO insert(Integer id, String url, HttpSession session) {
        User u = (User) session.getAttribute(UserSessionName.USER);
        Picture picture = new Picture();
        if (u == null) {
            return ResponseVO.error("请登录");
        }
        if (id != null && url != null) {
            picture.setUserId(u.getUserId());
            picture.setAddress(url);
            picture.setGoodId(id);
            picture.setTime(new Date());
            boolean save = pictureService.save(picture);
            return save ? ResponseVO.ok("成功") : ResponseVO.error("图片上传异常");
        }
        return ResponseVO.error("物品id或地址为空");
    }

}
